package Servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDao;
import model.PersonModel;

/** 
 * Servlet implementation class UpdateServlet
 */
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
	    int id =Integer.parseInt(request.getParameter("id"));
		String usename = request.getParameter("username");
		String password = request.getParameter("password");
		String sex = request.getParameter("sex");
		String hobbys[] = request.getParameterValues("hobby");
		String hobby="";
		for(String s:hobbys) {
			hobby+=s+" ";
		}
		
		String address = request.getParameter("address");
		
		PersonModel person = new PersonModel();
		person.setUsername(usename);
		person.setPassword(password);
		person.setId(id);
		person.setSex(sex);
		person.setHobby(hobby);
		person.setAddress(address);
		
		UserDao userDao =  new UserDao();
		
        userDao.updateInformation(person);
   
   
        
        ArrayList<PersonModel> list = userDao.selectAll();
      
        request.getSession().setAttribute("list", list);
        
        
        response.sendRedirect("/Test2022/web/js/showAll.jsp");
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
